#include "Event.h"
#include "IEventCallback.h"
#include "EventCallbackFunction.h"

#include <mutex>
#include <algorithm>

namespace Rz {

Event::Event()
{

}

Event::~Event()
{

}

Event& Event::operator += (IEventCallback::FunctionType fn)
{
	_events.push_back(new EventCallbackFunction(fn));
	return *this;
}

Event& Event::operator += (IEventCallback* fn)
{
	_events.push_back(fn);
	return *this;
}

Event& Event::operator -= (IEventCallback* fn)
{
	auto it =  std::find(_events.begin(), _events.end(), fn);

	if (it != _events.end())
	{
		_events.erase(it);
	}

	return *this;
}

void Event::operator () (Object& sender, EventArgs& e)
{
	for (auto& f : _events)
	{
		f->Callback(sender, e);
	}
}

}
